package com.hj.chartdemo.chart;

import android.content.Context;
import android.graphics.Typeface;

import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;

public class HBarChartManagger {

  private static HBarChartManagger instance = null;

  protected Typeface tfRegular;
  protected Typeface tfLight;

  private Context activity;

  public static HBarChartManagger getInstance() {
    if (instance == null) {
      instance = new HBarChartManagger();
    }
    return instance;
  }

  // 初始化
  public void init(Context context, HorizontalBarChart barChart) {
    instance.activity = context;

    tfRegular = Typeface.createFromAsset(activity.getAssets(), "OpenSans-Regular.ttf");
    tfLight = Typeface.createFromAsset(activity.getAssets(), "OpenSans-Light.ttf");

    //柱状图的阴影
    barChart.setDrawBarShadow(false);
    //设置柱状图Value值显示在柱状图上方 true 为显示上方，默认false value值显示在柱状图里面
    barChart.setDrawValueAboveBar(true);
    //Description Label 是否可见
    barChart.getDescription().setEnabled(false);
    // 二指控制X轴Y轴同时放大
    barChart.setPinchZoom(false);
    //是否显示表格背景颜色
    barChart.setDrawGridBackground(false);
    barChart.setDragEnabled(true);
    barChart.setScaleEnabled(true);

    // 显示的时候是按照多大的比率缩放显示,1f表示不放大缩小
//    barChart.zoom(1f, 1f, 0f, 0f);

    XAxis xAxis = barChart.getXAxis();
    //设置X轴显示位置
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setTypeface(tfLight);
    //X轴纵向分割线，一般不设置显示
//    xAxis.setDrawGridLines(true);
    xAxis.setDrawAxisLine(true);
    xAxis.setGranularity(1);
    xAxis.setAxisMinimum(0);

    YAxis leftAxis = barChart.getAxisLeft();
//    leftAxis.setTypeface(tfLight)
    //设置Y左边轴显示的值 label 数量
//        leftAxis.setLabelCount(8, false)
//        leftAxis.setValueFormatter(custom)
    //设置值显示的位置，我们这里设置为显示在Y轴外面
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setSpaceTop(15f);
    leftAxis.setSpaceBottom(5f);
    leftAxis.setAxisMinimum(0f);
    leftAxis.setGranularity(1f);

    YAxis rightAxis = barChart.getAxisRight();
    rightAxis.setEnabled(false);

    Legend l = barChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setForm(Legend.LegendForm.SQUARE);
    l.setFormSize(9f);
    l.setTextSize(11f);
    l.setXEntrySpace(4f);
    l.setEnabled(false);

    barChart.setNoDataText("暂无数据");

    barChart.setFitBars(true);
//    barChart.animateY(700);
    barChart.setVisibleXRangeMaximum(20);
  }

  public void loadData(
          Context context,
          HorizontalBarChart barChart,
          ArrayList<ArrayList<Entry>> datas,
          ArrayList<String> titles,
          ArrayList<Integer> colors,
          final ArrayList<String> xlab,
          Boolean showLegend,
          Boolean rotationAngle
          ) {


    barChart.getLegend().setEnabled(showLegend);

    BarDataSet set1 = null;

    float maxCount = 0f;
    ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
    for (int i = 0; i < datas.size(); i++) {
      ArrayList<BarEntry> values = new ArrayList<BarEntry>();
      ArrayList<Entry> d = datas.get(i);
      for (int j = 0; j < d.size(); j++) {
        values.add(new BarEntry(d.get(j).getX(),d.get(j).getY(),d.get(j).getData()));
        if(d.get(j).getY()>maxCount){
          maxCount = d.get(j).getY();
        }
      }
      String title = "";
      if (titles.size() - 1 >= i) {
        title = titles.get(i);
      }
      set1 = new BarDataSet(values,title);
      set1.setDrawIcons(false);
      if (colors.size() - 1 >= i) {
        set1.setColor(colors.get(i));
      }
      dataSets.add(set1);
    }

    BarData data = new BarData(dataSets);
    data.setValueTextSize(10f);

    if (xlab != null) {
      XAxis lineXAxis = barChart.getXAxis();
      lineXAxis.setValueFormatter(
              new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
//                    Log.d("xlab",xlab.size()+","+value);
//                    return value+"";
                  if ((int) (value-1) >= xlab.size() || value == 0) {
                    return "";
                  } else {
                    int tag = (int) (((value-1) * 10 + 5) / 10);
//                      Log.d("New_Fragment_Date", "value..==:" + (value-1));
//                      Log.d("New_Fragment_Date", "tag:" + tag+",lab:"+xlab.get(tag));
                    return xlab.get(tag) + "";
                  }
                }
              });
      lineXAxis.setLabelCount(xlab.size());

      if(rotationAngle){
//        lineXAxis.setLabelRotationAngle(20f);
        lineXAxis.setTextSize(8f);
      }else{
//        lineXAxis.setLabelRotationAngle(0);
        lineXAxis.setTextSize(10f);
      }
      //X轴的设置及自定义X轴
      BarAxisValueFormatter xAxisFormatter = new BarAxisValueFormatter(barChart,xlab);
      XYMarkerView mv = new XYMarkerView(context, xAxisFormatter);
      mv.setChartView(barChart); // For bounds control
      barChart.setMarker(mv); // Set the marker to the chart
      YAxis leftAxis = barChart.getAxisLeft();
      if(xlab.size()<5){
        data.setBarWidth(0.08f);

        leftAxis.setAxisMaximum((float)(maxCount*1.2));
      }else{
        data.setBarWidth(0.5f);
        leftAxis.resetAxisMaximum();
      }
    }else
      data.setBarWidth(0.5f);
//      data.setValueTypeface(tfLight)
    barChart.setData(data);
    barChart.invalidate();
  }
}
